package com.worksystem.Dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.worksystem.Common.BaseDao;
import com.worksystem.entity.Comment;
import com.worksystem.entity.MyArray;
import com.worksystem.entity.Task;
import com.worksystem.entity.User;

@Component
public class TaskDaoImp extends BaseDao<Task> implements TaskDao{

	@Autowired
	private SessionFactory sessionFactory ;
	
	@Override
	public List<Task> getStudentTasklist(User user,int start,int length) {
		String hql1="select t from Task as t,Pokermans as p "
				+ "where p.user.uid=:uid and t.tid=p.task.tid";
		Query query=getSession().createQuery(hql1).setParameter("uid", user.getUid()).setMaxResults(length);
		List<Task> tasks=query.list();//getObjectsByhqlParameter(query, parameters);
		return tasks;
	}
	@Override
	public List<Task> getTeacherTasklist(User user,int start,int length){
		String hql="from Task where user.uid=:uid";
		//System.out.println("开始执行教师查询列表"+user.getUid());
		HashMap<String, Object> parameter=new HashMap<String, Object>();
		parameter.put("uid", user.getUid());
		Query query=getEntityByHql(hql,parameter, start, length);
		return query.list();//getSession().createQuery(hql).list();
	}
	
	@Override
	public User getUserByTaskId(int tid) {
		String hql="select Task.user from Task  where tid=:tid";
		Query query=getSession().createQuery(hql).setParameter("tid", tid);
		List<User> user=query.list();
		return user.get(0);
	}

	@Override
	public Task getTaskById(Integer id) {
		Task task=(Task) getSession().get(Task.class, id);
		return task;
	}

	@Override
	public List<Comment> getCommentsByTaskId(Integer id) {
		String hql="from Comment where task.tid=:tid";
		List<Comment> comments=getSession().createQuery(hql).setParameter("tid", id).list();
		return comments;
		
	}

	@Override
	public void saveComment(Comment comment) {
		getSession().save(comment);
		Task task=comment.getTask();
		task=getTaskById(task.getTid());
		//评论+1
		System.out.println("当前评论数"+task.getComment()+"评论+1");
		String sql="insert into ";
		//更新任务
		getSession().update(task);
	}
	
	@Override
	public Integer inserttask(Task task,Object[] pokermanids){
		try {
			save(task);
			Integer tid=task.getTid();
			for (Object uid : pokermanids) {
				String sql="INSERT INTO POKERMANS(PID,USER,TASK) VALUES (NULL, '"+uid+"', '"+tid+"')";
				if(getSession().createSQLQuery(sql).executeUpdate()>0){
					System.out.println("插入关注人成功");
				}
				else{
					System.out.println("插入关注人失败，返回错误代码104");
					return 104;
				}
			}
		} catch (Exception e) {
			System.out.println("插入任务失败，返回代码103");
			return 103;
		}
		return 1;
	}
	
	@Override
	public Integer deletetask(Integer tid){
		//Task task=getTaskById(tid);
		getSession().beginTransaction();
		try {
			String hql="delete Comment where task.tid=:tid";
			String pokermanhql="delete Pokermans where task.tid=:tid";
			getSession().createQuery(pokermanhql).setParameter("tid", tid).executeUpdate();
			getSession().createQuery(hql).setParameter("tid", tid).executeUpdate();
			String hql2="delete Task where tid=:tid ";
			getSession().createQuery(hql2).setParameter("tid", tid).executeUpdate();
			getSession().getTransaction().commit();
			return 1;
		} catch (Exception e) {
			System.out.println("任务删除失败，返回错误105");
			getSession().getTransaction().rollback();
			return 105;
		}
	}
	
	@Override
	public Integer updatetask(Task task,Object[] pokermanids){
		//首先删除pokermans里面的关注列表，然后再赋值新的进去
		getSession().beginTransaction();
		try {
			getSession().saveOrUpdate(task);
			getSession().flush();
			//删除
			String pokermanhql="delete Pokermans where task.tid=:tid";
			getSession().createQuery(pokermanhql).setParameter("tid", task.getTid()).executeUpdate();
			//插入
			for (Object uid : pokermanids) {
				String sql="INSERT INTO POKERMANS(PID,USER,TASK) VALUES (NULL, '"+uid+"', '"+task.getTid()+"')";
				if(getSession().createSQLQuery(sql).executeUpdate()>0){
					System.out.println("插入关注人成功");
				}
				else{
					System.out.println("插入关注人失败，返回错误代码104");
					return 104;
				}
			}
			getSession().getTransaction().commit();
		} catch (Exception e) {
			System.out.println("更新任务失败，返回错误代码106");
			getSession().getTransaction().rollback();
			return 106;
		}
		
		return 1;
	}
}
